<?php

class Email
{
	var $error;

	function get_contacts($account, $password)
	{
		$contacts = grap_163($account, $password);
		if (!is_array($contacts)) {
			$this->error = $contacts;
			return false;
		}
		return $contacts;
	}

	function get_error()
	{
		return $this->error;
	}
}


/**
 * grap contacter_list from mail.sina.com
 * create by liuzhiguo clayton.liu@gmail.com
 * 2008-8-20 16:18 1.0 $
 */

/*
$user = trim($_POST['username']);
$password = trim($_POST['password']);

$contacter_list = grap_sina($user, $password);
*/

/**
 * grap contacters from mail.sina.com
 *
 * @param string $user
 * @param string $password
 * @return array $contacter_list
 */
function grap_163($user, $password){
	if (strpos($user, '@') !== false) {
		$user = current(explode('@', $user));
	}

	$tmp_dir = PATH_SEPARATOR == ':' ? '/dev/shm/invite/' : 'c:/windows/temp/';
	$tmp_dir = $tmp_dir . substr(md5(microtime(true) . rand(0, 999999999)), 0, 2);

	$cookie_jar = tempnam($tmp_dir, 'cookie');

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_TIMEOUT, 3);
	curl_setopt($ch, CURLOPT_URL, 'http://reg.163.com/login.jsp?type=1&url=http://fm163.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D16');
	curl_setopt($ch, CURLOPT_POST, 1);
	$request = 'verifycookie=1&username='.$user.'&password='.$password.'&selType=jy&%B5%C7%C2%BC%D3%CA%CF%E4=%B5%C7%C2%BC%D3%CA%CF%E4&style=16&product=mail163';
	curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_NOBODY, false);
	$html=curl_exec($ch);
	curl_close($ch);

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_TIMEOUT, 3);
	curl_setopt($ch, CURLOPT_URL, 'http://fm163.163.com/coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=16&username='. $user);
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
	$html=curl_exec($ch);
	curl_close($ch);
	
	//<a href="http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight=1&amp;verifycookie=1&amp;language=-1&amp;style=16&amp;username=sdotesttest">here</a>
	
	preg_match_all('/<p>The document has moved <a href="(.*?)">here<\/a>\.<\/p>/', $html, $m1);
//	var_dump($m1);
	
	if(strlen($m1[1][0]) == 0)
	{
		unlink($cookie_jar);
		return 'err_01';
	}
	
//	$find1   = 'sid=';
//	$find2   = '">here';
//	$pos1 = strpos($html, $find1);
//	$pos2 = strpos($html, $find2);
//	$l=$pos2-$pos1-4;
//	// echo substr($html, $pos1+4,$l);
//	$sess=substr($html, $pos1+4,$l);
//	 var_dump($html);
//	$sess=trim($sess);
//	
//	var_dump($sess);
//	
//	if (strlen($sess) > 32 || strlen($sess) < 6) {
//		return 'err_01';
//	}

	$ch2 = curl_init();
	curl_setopt($ch2, CURLOPT_TIMEOUT, 3);
	curl_setopt($ch2, CURLOPT_URL, $m1[1][0]);
	curl_setopt($ch2, CURLOPT_HEADER, false);
	curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar);
	curl_setopt($ch2, CURLOPT_COOKIEJAR, $cookie_jar);
	$html = curl_exec($ch2);
	$html=str_replace("\n","",$html); 
	curl_close($ch2);
	
	preg_match_all('/<p>The document has moved <a href="(.*?)">here<\/a>\.<\/p>/', $html, $m2);
	
	$find1   = 'sid=';
	$find2   = '">here';
	$pos1 = strpos($html, $find1);
	$pos2 = strpos($html, $find2);
	$l=$pos2-$pos1-4;
	$sess=substr($html, $pos1+4,$l);
	$sess=trim($sess);
	
	if (strlen($sess) > 32 || strlen($sess) < 6) {
		unlink($cookie_jar);
		return 'err_01';
	}
	
	$list = parse_url($m2[1][0]);
	
	if(strlen($list['scheme']) == 0 || strlen($list['host']) == 0)
	{
		unlink($cookie_jar);
		return 'error_01';
	}
	
	$ch3 = curl_init();
	curl_setopt($ch3, CURLOPT_TIMEOUT, 3);
	curl_setopt($ch3, CURLOPT_URL, 'http://' . $list['host'] . '/js3/s?sid=' . $sess . '&func=global:sequential');
	curl_setopt($ch3, CURLOPT_HEADER, false);
	curl_setopt($ch3, CURLOPT_HTTPHEADER, array( "Content-Type: application/xml" ) );
	curl_setopt($ch3, CURLOPT_REFERER, $m2[1][0]);
	curl_setopt($ch3, CURLOPT_RETURNTRANSFER, 1);
	
	$str = "<?xml version=\"1.0\"?><object><array name=\"items\"><object><string name=\"func\">pab:searchContacts</string>" .
               "<object name=\"var\"><array name=\"order\"><object><string name=\"field\">FN</string><boolean name=\"ignoreCase\">true</boolean></object>" .
               "</array></object></object><object><string name=\"func\">user:getSignatures</string></object>" .
               "<object><string name=\"func\">pab:getAllGroups</string></object></array></object>";
     curl_setopt( $ch3, CURLOPT_POST, 1 );
     curl_setopt( $ch3, CURLOPT_POSTFIELDS, $str );
	curl_setopt($ch3, CURLOPT_COOKIEFILE, $cookie_jar);
	curl_setopt($ch3, CURLOPT_COOKIEJAR, $cookie_jar);

	$html = curl_exec($ch3);
	$html=str_replace("\n","",$html); 
	curl_close($ch3);

	preg_match_all('/<string name="EMAIL;PREF">(.*?)<\/string>/', $html, $name2);
  	preg_match_all('/<string name="FN">(.*?)<\/string>/', $html, $name1);

	$nicknames = $name1[1];
	$accounts = $name2[1];
	$contacts = array();

	foreach ($nicknames as $k => $v) {
		$tmp['name'] = $v;
		$tmp['email'] = $accounts[$k];
		array_push($contacts, $tmp);
	}

	unlink($cookie_jar);

	if (!count($contacts)) {
		return 'err_02';
	}
		
	return $contacts;
}
?>